package cn.community.care.module.system.service.follow;

import cn.community.care.module.system.controller.admin.follow.vo.FollowPageReqVO;
import cn.community.care.module.system.controller.admin.follow.vo.FollowSaveReqVO;
import cn.community.care.module.system.dal.dataobject.follow.FollowDO;
import cn.community.care.module.system.dal.mysql.follow.FollowMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.community.care.framework.common.pojo.PageResult;
import cn.community.care.framework.common.pojo.PageParam;
import cn.community.care.framework.common.util.object.BeanUtils;


import javax.annotation.Resource;

import static cn.community.care.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 用户关注关系 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class FollowServiceImpl implements FollowService {

    @Resource
    private FollowMapper followMapper;

    @Override
    public Long createFollow(FollowSaveReqVO createReqVO) {
        // 插入
        FollowDO follow = BeanUtils.toBean(createReqVO, FollowDO.class);
        followMapper.insert(follow);
        // 返回
        return follow.getId();
    }

    @Override
    public void updateFollow(FollowSaveReqVO updateReqVO) {
        // 校验存在
        validateFollowExists(updateReqVO.getId());
        // 更新
        FollowDO updateObj = BeanUtils.toBean(updateReqVO, FollowDO.class);
        followMapper.updateById(updateObj);
    }

    @Override
    public void deleteFollow(Long id) {
        // 校验存在
        validateFollowExists(id);
        // 删除
        followMapper.deleteById(id);
    }

    private void validateFollowExists(Long id) {
        if (followMapper.selectById(id) == null) {
//            throw exception(FOLLOW_NOT_EXISTS);
        }
    }

    @Override
    public FollowDO getFollow(Long id) {
        return followMapper.selectById(id);
    }

    @Override
    public PageResult<FollowDO> getFollowPage(FollowPageReqVO pageReqVO) {
        return followMapper.selectPage(pageReqVO);
    }

}